<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="场景名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入场景名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi-device:managementScene:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <el-row>
      <el-col
        v-for="(item, index) in managementSceneList"
        :key="item.id"
        :span="4"
        :offset="index % 5 !== 0 ? 1 : 0"
        style="margin-bottom: 15px"
      >
        <el-card :body-style="{ padding: '10px' }">
          <template #header>
            <div class="card-header" style="color: blue">
              <span>{{ item.systemName }}</span>
            </div>
            <div class="card-header">
              <span>{{ item.name }}</span>
            </div>
          </template>
          <div
            style="display: flex; align-items: center; justify-content: center"
            title="点击预览"
            @click="handleToView(item)"
          >
            <img :src="item.thumbnail" class="image" />
          </div>
          <div
            style="display: flex; margin-top: 10px; justify-content: flex-end"
          >
            <el-link
              type="primary"
              @click="handleEdit(item)"
              style="margin-right: 8px"
              >设计</el-link
            >
            <el-link
              type="success"
              @click="handleUpdate(item)"
              style="margin-right: 8px"
              v-hasPermi="['ruoyi-device:managementScene:edit']"
              >修改</el-link
            >
            <el-link
              type="danger"
              @click="handleDelete(item)"
              v-hasPermi="['ruoyi-device:managementScene:remove']"
              >删除</el-link
            >
          </div>
        </el-card>
      </el-col>
    </el-row>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场景管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="managementSceneRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="子系统" prop="sid">
          <el-select v-model="form.sid" placeholder="请选择子系统" filterable>
            <el-option
              v-for="item in optionsManagerSystem"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="场景名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入场景名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="略缩图">
          <ImageUpload
            :limit="1"
            v-model="form.thumbnail"
            :showIcon="true"
          ></ImageUpload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<style scoped>
/* 列表展示 */
.image {
  width: 150px;
  height: 150px;
  cursor: pointer;
}

.toolbar {
  display: flex;
  margin-left: auto;
  margin-right: 5px;
}
</style>
<script setup name="ManagementScene">


import { useRouter } from "vue-router";
const router = useRouter();

function handleEdit(row) {
  router.push({
    path: "maotuPage",
    query: {
      id: row.id,
    },
  });
}
function  handleToView(row) {
  router.push({
    path: "maotuView",
    query: {
      id: row.id,
    },
  });
}

const arrayManagerSystem = ref([]);
const optionsManagerSystem = ref([]);
function getoptionsManagerSystem(tempList) {
  optionsManagerSystem.value = [];
  tempList.forEach((element) => {
    optionsManagerSystem.value.push({
      label: element.name,
      value: element.id,
    });
  });
}
function getManagerList() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listManagerSystem(queryParams1).then((response) => {
    console.log(response);
    arrayManagerSystem.value = response.rows;
    getoptionsManagerSystem(response.rows);
    getList();
  });
}

onMounted(() => {
  getManagerList();
});

import {
  listManagementScene,
  getManagementScene,
  delManagementScene,
  addManagementScene,
  updateManagementScene,
} from "@/api/ruoyi-device/managementScene";
import { listManagerSystem } from "@/api/ruoyi-device/managerSystem";
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();

const managementSceneList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询场景管理列表 */
function getList() {
  loading.value = true;
  listManagementScene(queryParams.value).then((response) => {
    managementSceneList.value = response.rows;
    console.log(managementSceneList.value);
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    sid: null,
    name: null,
    remark: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    delFlag: null,
  };
  proxy.resetForm("managementSceneRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加场景管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getManagementScene(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改场景管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["managementSceneRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateManagementScene(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManagementScene(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除场景管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delManagementScene(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-device/managementScene/export",
    {
      ...queryParams.value,
    },
    `managementScene_${new Date().getTime()}.xlsx`
  );
}
</script>
